import translateengine from './translateengine.js';
import graphobjpath from "./graphobjpath";

import rotateengine from "./rotateengine";
import rotatepath from "./rotatepath";

import transformanimateengine from './transformanimateengine.js';
import inflateengine from './inflateengine.js';
import pistonEngine from './pistonEngine.js';

class AnimateManager {
    constructor() {
        this.animateEngineList = new Array();
        this.m_preDefEngine = null;
    }

    animate() {
        let flag = false;
        let list = this.animateEngineList;
        for (let i = 0; i < list.length; i++) {
            let engine = list[i];
            engine.animate();
            flag = true;
        }
        return flag;
    }


    setAnimate(type, info) {
        let flag = false;
        if (type == "rotate") {
            this.setRotatetAnimate(info);
            flag = true;
        }
        else if (type == "inflate") {
            this.setInflateAnimate(info);
            flag = true;
        }
        else if (type == "piston") {
            this.setPistonAnimate(info);
            flag = true;
        }
        return flag;
    }

    //let animateInfo = { obj: destList.value, speed:pistonSpeed ,stPt,endPt,basePt};
    setPistonAnimate(info) {
        let graph = info.destGraph;

        let pAnimationEngine = new pistonEngine.PistonEngine(graph);
        pAnimationEngine.speed = info.speed;
        pAnimationEngine.setCtrlPoint(info.stPt,info.endPt);
        pAnimationEngine.setBasePoint(info.basePt);
        if (pAnimationEngine) {
            this.animateEngineList.push(pAnimationEngine);
        }
        return true;
    }

    //let animateInfo = { obj: destList.value, speed:inflateSpeed , max: maxScale.value,min:minScale.value}
    setInflateAnimate(info) {
        let graph = info.destGraph;

        let pAnimationEngine = new inflateengine.InflateEngine(graph);
        pAnimationEngine.speed = info.speed;
        pAnimationEngine.maxScale = info.max;
        pAnimationEngine.minScale = info.min;
        if (pAnimationEngine) {
            this.animateEngineList.push(pAnimationEngine);
        }
        return true;
    }

    //let rotInfo = { obj: rotDestList.value,pathObj:rotPathObj.value, speed: speed.value, rotCnt: mousePt }
    setRotatetAnimate(info) {
        let graph = info.destGraph;

        let pAnimationEngine = new rotateengine.RotateEngine(graph);
        pAnimationEngine.speed = info.speed;
        let path = new rotatepath.RotatePath(info.rotCnt, info.pathGraph);
        pAnimationEngine.setPath(path);

        if (pAnimationEngine) {
            this.animateEngineList.push(pAnimationEngine);
        }
        return true;
    }

    setAnimate_bak(cmd, graph) {
        if (!graph) {
            return false;
        }

        let setDstFlag = false;
        let type = "undefined";
        if (cmd == "transdest") {
            type = "translate";
            setDstFlag = true;
        }
        else if (cmd == "rotatedest") {
            type = "rotate";
            setDstFlag = true;
        }
        else if (cmd == "formdest") {
            type = "transform";
            setDstFlag = true;
        }
        else if (cmd == "transpath") {
            type = "translate";
        }
        else if (cmd == "rotatepath") {
            type = "rotate";
        }
        else {
            return false;
        }

        if (setDstFlag) {
            this.setAnimateDstGraph(graph, type);
        }
        else {
            this.setAnimatePath(graph, type);
        }
        return true;
    }

    setAnimateDstGraph(graph, type) {
        let pAnimationEngine = this.getSameEngine(graph, type);
        if (!pAnimationEngine) {
            if (type == "translate") {
                pAnimationEngine = new translateengine.TranslateEngine(graph);
            }
            else if (type == "rotate") {
                pAnimationEngine = new rotateengine.RotateEngine(graph);
            }
            else if (type == "transform") {
                pAnimationEngine = new transformanimateengine.TransformEngine(graph);
            }
            if (pAnimationEngine) {
                this.animateEngineList.push(pAnimationEngine);
                this.m_preDefEngine = pAnimationEngine;
            }
        }
        else {
            this.m_preDefEngine = pAnimationEngine;
        }
    }

    setAnimatePath(graph, type) {
        if (this.m_preDefEngine) {
            if (type == "translate") {
                let path = new graphobjpath.GraphObjPath(graph);
                this.m_preDefEngine.setPath(path);
            }
            else if (type == "rotate") {
                let path = new rotatepath.RotatePath(graph);
                this.m_preDefEngine.setPath(path);
            }
        }
    }

    getSameEngine(dstgraph, type) {
        let pSameEngine = null;
        let list = this.animateEngineList;
        for (let i = 0; i < list.length; i++) {
            let engine = list[i];
            if (engine.getDstGraph() == dstgraph && type == engine.type) {
                pSameEngine = engine;
                break;
            }
        }
        return pSameEngine;
    }

}

export default { AnimateManager }